
package jz;

import jz.dataCenter.faceLog.FaceLogMgr;
import jz.dataCenter.fileProcess.JzMachineLogFileProcess;
import jz.dataCenter.fileProcess.JzTabletVideoFileProcess;
import jz.service.queue.MachineMgr;
import jz.service.queue.ProviderQueue;
import jz.service.queue.QueueRefresh;
import jz.service.svr.JzJsonSubmitServlet;
import jz.service.svr.JzQueueServlet;
import jz.utils.JzConstants;
import jzQueueMgr.service.JzEnginServlet;
import jzQueueMgr.service.JzSubmitServlet;

import org.jdom.Element;

import SFPM.sysReport.ISystemStateReport;
import SoftFan.Log.wf_Log;
import SoftFan.dataCenter.FileProcessThread;
import SoftFan.util.systemUtil;

public class JzProcEngin implements ISystemStateReport {

	public static JzProcEngin	instance;

	public static JzProcEngin getInstance() {
		return instance;
	}

	public static void setInstance(JzProcEngin instance) {
		JzProcEngin.instance = instance;
	}

	private FileProcessThread	machineLogFileProcessThread		= null;
	private FileProcessThread	tabletVideoFileProcessThread	= null;

	private QueueRefresh		queueRefresh					= null;

	public JzProcEngin() {
		super();
		JzProcEngin.setInstance(this);
		JzEnginServlet.setJzQueueMgr(new JzQueueServlet());
		JzSubmitServlet.setJzSubmitMgr(new JzJsonSubmitServlet());
	}

	public void startup() {
		try {
			if (machineLogFileProcessThread == null) {
				machineLogFileProcessThread = new FileProcessThread("log", new JzMachineLogFileProcess());
				machineLogFileProcessThread.start();
			}
			if (tabletVideoFileProcessThread == null) {
				tabletVideoFileProcessThread = new FileProcessThread("pb", new JzTabletVideoFileProcess());
				tabletVideoFileProcessThread.setExcludeStartHoure(JzConstants.getInstance().getVideoBackJobStart());
				tabletVideoFileProcessThread.setExcludeEndHoure(JzConstants.getInstance().getVideoBackJobEnd());
				tabletVideoFileProcessThread.start();
			}

			MachineMgr.getInstance().initialMachine();
			ProviderQueue.getInstance().initialProvider();

			if (queueRefresh == null) {
				queueRefresh = new QueueRefresh();
				queueRefresh.start();
			}
			
			FaceLogMgr.getInstance().startup();
		} catch (java.lang.Exception e) {
			wf_Log.sys_log(systemUtil.getErrorMessage(e));
		} catch (java.lang.Throwable e) {
			wf_Log.sys_log(systemUtil.getErrorMessage(e));
		}
	}

	public void shutdown() {
		if (queueRefresh == null) {
			queueRefresh.shutdown();
			queueRefresh = null;
		}
		if (machineLogFileProcessThread != null) {
			machineLogFileProcessThread.doDone();
			machineLogFileProcessThread = null;
		}
		if (tabletVideoFileProcessThread != null) {
			tabletVideoFileProcessThread.doDone();
			tabletVideoFileProcessThread = null;
		}
		
		FaceLogMgr.getInstance().shutdown();
	}

	public void outInfo(Element errContent, Element infoContent) {
	}

}
